{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# C++的结构体"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1. 结构体"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 结构体属于用户自定义的数据类型，允许用户存储不同的数据类型。\n",
    "\n",
    "② 语法：struct 结构体名 { 结构体成员列表 }\n",
    "\n",
    "③ 通过结构体创建变量的方式有三种：\n",
    "\n",
    "1. struct 结构体名 变量名\n",
    "2. struct 结构体名 变量名 = { 成员1值，成员2值 }\n",
    "3. 定义结构体时顺便创建变量\n",
    "\n",
    "④ 结构体变量利用点.访问成员"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "//自定义数据类型，一些类型的集合组成一个类型\n",
    "//语法 struct 类型名称 { 成员列表 }\n",
    "//结构体定义的时候，struct关键字不能省略\n",
    "//1、创建学生数据类型：学生包括(姓名，年龄，分数)\n",
    "struct Student\n",
    "{\n",
    "    //成员列表\n",
    "\n",
    "    //姓名\n",
    "    string name;\n",
    "    //年龄\n",
    "    int age;\n",
    "    //分数\n",
    "    int score;\n",
    "};\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //2.1 struct Student s1  s1类似结构体的实例值，类似变量赋值：int a = 10 → 属性 变量 变量值\n",
    "    //结构体创建的时候，struct 关键字可以省略；上面结构体定义的时候 struct 可以省略\n",
    "    struct Student s1;\n",
    "    //给s1属性赋值，通过点.访问结构体变量中的属性\n",
    "    s1.name = \"张三\";\n",
    "    s1.age = 18;\n",
    "    s1.score = 100;\n",
    "\n",
    "    cout << \"姓名：\" << s1.name << \"年龄:\" << s1.age << \"分数：\" << s1.score << endl;\n",
    "\n",
    "    //2.2 struct Student s2 = { ... }\n",
    "    struct Student s2 = { \"李四\",19,80 };\n",
    "    cout << \"姓名：\" << s2.name << \"年龄:\" << s2.age << \"分数：\" << s2.score << endl;\n",
    "    \n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 姓名：张三年龄:18分数：100  \n",
    " - 姓名：李四年龄:19分数：80  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "//自定义数据类型，一些类型的集合组成一个类型\n",
    "struct Student\n",
    "{\n",
    "    //成员列表\n",
    "\n",
    "    //姓名\n",
    "    string name;\n",
    "    //年龄\n",
    "    int age;\n",
    "    //分数\n",
    "    int score;\n",
    "}s3;   //2.3 创建结构体的时候，顺便创建个结构体变量\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //2.3 \n",
    "    s3.name = \"王五\";\n",
    "    s3.age = 20;\n",
    "    s3.score = 60;\n",
    "\n",
    "    cout << \"姓名：\" << s3.name << \"年龄:\" << s3.age << \"分数：\" << s3.score << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 姓名：王五年龄:20分数：60  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.1 结构体定义和使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：将自定义的结构体放入到数组中方便维护。\n",
    "\n",
    "② 语法：struct 结构体名 数组名[元素个数] = { {},{},...,{} }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "//自定义数据类型，一些类型的集合组成一个类型\n",
    "struct Student\n",
    "{\n",
    "    //成员列表\n",
    "\n",
    "    //姓名\n",
    "    string name;\n",
    "    //年龄\n",
    "    int age;\n",
    "    //分数\n",
    "    int score;\n",
    "};  \n",
    "\n",
    "int main()\n",
    "{\n",
    "    //2、创建结构体数组\n",
    "    struct Student stuArray[3] =\n",
    "    {\n",
    "        {\"张三\",18,100},\n",
    "        {\"李四\",28,99},\n",
    "        {\"王五\",38,66}\n",
    "    };\n",
    "\n",
    "    //3、给结构体数组中的元素赋值\n",
    "    stuArray[2].name = \"赵六\";\n",
    "    stuArray[2].age = 80;\n",
    "    stuArray[2].score = 60;\n",
    "\n",
    "    //4、遍历结构体数组\n",
    "    for (int i = 0; i < 3; i++)\n",
    "    {\n",
    "        cout << \"姓名：\" << stuArray[i].name << \"年龄：\" << stuArray[i].age << \"分数：\" << stuArray[i].score << endl;\n",
    "    }\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 姓名：张三年龄：18分数：100  \n",
    " - 姓名：李四年龄：28分数：99  \n",
    " - 姓名：赵六年龄：80分数：60\n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.2 结构体数组"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：将自定义的结构体放入到数组中方便维护。\n",
    "\n",
    "② 语法：struct 结构体名 数组名[元素个数] = { {},{},...,{} }"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "//自定义数据类型，一些类型的集合组成一个类型\n",
    "struct Student\n",
    "{\n",
    "    //成员列表\n",
    "\n",
    "    //姓名\n",
    "    string name;\n",
    "    //年龄\n",
    "    int age;\n",
    "    //分数\n",
    "    int score;\n",
    "};\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //2、创建结构体数组\n",
    "    struct Student stuArray[3] =\n",
    "    {\n",
    "        {\"张三\",18,100},\n",
    "        {\"李四\",28,99},\n",
    "        {\"王五\",38,66}\n",
    "    };\n",
    "\n",
    "    //3、给结构体数组中的元素赋值\n",
    "    stuArray[2].name = \"赵六\";\n",
    "    stuArray[2].age = 80;\n",
    "    stuArray[2].score = 60;\n",
    "\n",
    "    //4、遍历结构体数组\n",
    "    for (int i = 0; i < 3; i++)\n",
    "    {\n",
    "        cout << \"姓名：\" << stuArray[i].name << \"年龄：\" << stuArray[i].age << \"分数：\" << stuArray[i].score << endl;\n",
    "    }\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 姓名：张三年龄：18分数：100  \n",
    " - 姓名：李四年龄：28分数：99  \n",
    " - 姓名：赵六年龄：80分数：60  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.3 结构体指针"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：通过指针访问结构体中的成员。\n",
    "\n",
    "② 利用操作符 · > 可以通过结构体指针访问结构体属性。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "\n",
    "//结构体指针\n",
    "\n",
    "//定义学生结构体\n",
    "struct Student\n",
    "{\n",
    "    string name; //姓名\n",
    "    int age; //年龄\n",
    "    int score; //分数\n",
    "};\n",
    "\n",
    "int main()\n",
    "{\n",
    "    // 1、创建学生结构体变量，这里的 struct 可以省略\n",
    "    struct Student s = { \"张三\",18,100 };\n",
    "\n",
    "    //2、通过指针指向结构体变量\n",
    "    struct Student* p = &s;  //对s取地址， tudent * p 类似 int * p，这里的 struct 可以省略\n",
    "\n",
    "    //3、通过指针访问结构体变量中的数据\n",
    "    //通过结构体指针 访问结构体中的属性，需要利用'->'\n",
    "    cout << \"姓名：\" << p->name << \"年龄：\" << p->age << \"分数：\" << p->score << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    "- 姓名：张三年龄：18分数：100  \n",
    "- 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.4 结构体嵌套结构体"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 结构体中的成员可以是另一个结构体。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "\n",
    "//因为老师的结构体里有学生的结构体，所以学生结构体要在老师结构体前面先定义\n",
    "//定义学生结构体\n",
    "struct student\n",
    "{\n",
    "    string name; //姓名\n",
    "    int age; //年龄\n",
    "    int score; //分数\n",
    "}; \n",
    "\n",
    "//定义老师结构体\n",
    "struct teacher\n",
    "{\n",
    "    int id; //教师编号\n",
    "    string name; //教师姓名\n",
    "    int age; //年龄\n",
    "    struct student stu; //学生结构体\n",
    "};\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //结构体嵌套结构体\n",
    "    //创建老师\n",
    "    teacher t;\n",
    "    t.id = 10000;\n",
    "    t.name = \"老王\";\n",
    "    t.age = 50;\n",
    "    t.stu.name = \"小王\";\n",
    "    t.stu.age = 20;\n",
    "    t.stu.score = 60;\n",
    "\n",
    "    cout << \"老师姓名：\" << t.name << \"老师编号：\" << t.id << \"老师年龄：\" << t.age\n",
    "        << \"老师辅导的学生姓名：\" << t.stu.name << \"学生年龄：\" << t.stu.age \n",
    "        << \"学生考试分数：\" << t.stu.score << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 老师姓名：老王 老师编号：10000 老师年龄：50 老师辅导的学生姓名：小王 学生年龄：20 学生考试分数：60\n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.5 结构体做函数参数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：将结构体作为参数向函数中传递，传递方式有两种。\n",
    "\n",
    "② 传递方式有两种：\n",
    "\n",
    "1. 值传递\n",
    "2. 地址传递"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "\n",
    "//定义学生结构体\n",
    "struct student\n",
    "{\n",
    "    string name; //姓名\n",
    "    int age; //年龄\n",
    "    int score; //分数\n",
    "};\n",
    "\n",
    "//定义老师结构体\n",
    "struct teacher\n",
    "{\n",
    "    int id; //教师编号\n",
    "    string name; //教师姓名\n",
    "    int age; //年龄\n",
    "    struct student stu; //学生结构体\n",
    "};\n",
    "\n",
    "//打印学生信息的函数\n",
    "//1、值传递\n",
    "void printStudent1(struct student s)\n",
    "{\n",
    "    cout << \"子函数 值传递前 姓名：\" << s.name << \"年龄：\" << s.age << \"分数：\" << s.score << endl;\n",
    "\n",
    "    s.age = 100;\n",
    "}\n",
    "\n",
    "//2、地址传递\n",
    "void printStudent2(struct student* p)\n",
    "{\n",
    "    cout << \"子函数 地址传递前 姓名：\" << p->name << \"年龄：\" << p->age << \"分数：\" << p->score << endl;\n",
    "\n",
    "    p->score = 90;\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //结构体做函数参数\n",
    "    //将学生传入到一个参数中，打印学生身上的所有信息\n",
    "\n",
    "    //创建结构体变量\n",
    "    struct student s;\n",
    "    s.name = \"张三\";\n",
    "    s.age = 20;\n",
    "    s.score = 85;\n",
    "\n",
    "    cout << \"main函数 传递前 姓名：\" << s.name << \"年龄：\" << s.age << \"分数：\" << s.score << endl;\n",
    "\n",
    "    printStudent1(s);\n",
    "\n",
    "    cout << \"子函数  值传递后 姓名：\" << s.name << \"年龄：\" << s.age << \"分数：\" << s.score << endl;\n",
    "\n",
    "    printStudent2(&s);\n",
    "\n",
    "    cout << \"子函数 地址传递后 姓名：\" << s.name << \"年龄：\" << s.age << \"分数：\" << s.score << endl;\n",
    "\n",
    "\n",
    "    cout << \"main函数 传递后 姓名：\" << s.name << \"年龄：\" << s.age << \"分数：\" << s.score << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - main函数 传递前 姓名：张三年龄：20分数：85  \n",
    " - 子函数 值传递前 姓名：张三年龄：20分数：85  \n",
    " - 子函数  值传递后 姓名：张三年龄：20分数：85  \n",
    " - 子函数 地址传递前 姓名：张三年龄：20分数：85  \n",
    " - 子函数 地址传递后 姓名：张三年龄：20分数：90  \n",
    " - main函数 传递后 姓名：张三年龄：20分数：90  \n",
    " - 请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.6 结构体中const使用"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "① 作用：const来防止误操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "\n",
    "//const 的使用场景\n",
    "\n",
    "struct student\n",
    "{\n",
    "    //姓名\n",
    "    string name;\n",
    "    //年龄\n",
    "    int age;\n",
    "    //分数\n",
    "    int score;\n",
    "};\n",
    "\n",
    "//将函数中形参设置为指针，用地址传递，而不是值传递，可以减少内存空间，而且不会复制新的副本\n",
    "//值传递需要复制新的副本，如果有成千上万个学生调用结构体，会复制成千上个副本\n",
    "void printStudents(const student* s)\n",
    "{\n",
    "    s->age = 150;  //报错，因为假如const之后，一旦有修改的操作就会报错，可以防止我们的误操作。\n",
    "    cout << \"姓名：\" << s->name << \"年龄：\" << s->age << \"分数：\" << s->score << endl;\n",
    "}\n",
    "\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //创建结构体变量\n",
    "    struct student s = { \"张三\",15,70 };\n",
    "\n",
    "    //通过函数打印结构体变量信息\n",
    "    printStudents(&s);\n",
    "\n",
    "    cout << \"main中张三年龄为：\" << s.age << endl;\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.7 结构体案例(一)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "案例描述：学校正在做毕设项目，每名老师带领5个学生，总共有3名老师，需求如下：设计学生和老师的结构体，其中在老师的结构体中，有老师姓名和一个存放5名学生的数组作为成员，学生的成员有姓名、考试分数、创建数组存放3名老师，通过函数给每个老师及所带的学生赋值。最终打印出老师数据以及老师所带的学生数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "#include <string>\n",
    "#include <Ctime>\n",
    "\n",
    "//学生结构体定义\n",
    "struct Student\n",
    "{\n",
    "    //姓名\n",
    "    string sName;\n",
    "    //学生数组\n",
    "    int score;\n",
    "};\n",
    "\n",
    "//老师结构体\n",
    "struct Teacher\n",
    "{\n",
    "    //姓名\n",
    "    string tName;\n",
    "    //学生数组\n",
    "    struct Student sArray[5];\n",
    "};\n",
    "\n",
    "//给老师和学生赋值的函数\n",
    "void allocateSpace(struct Teacher tArray[], int len) //接收数组的两种方式：一用指针，二用数组名 struct Teacher tArray[]\n",
    "{\n",
    "    string nameSeed = \"ABCDE\";\n",
    "    //给老师开始赋值\n",
    "    for (int i = 0; i < len; i++)\n",
    "    {\n",
    "        tArray[i].tName = \"Teacher_\";\n",
    "        tArray[i].tName += nameSeed[i];\n",
    "\n",
    "        //通过循环给每名老师所带的学生赋值\n",
    "        for (int j = 0; j < 5; j++)\n",
    "        {\n",
    "            tArray[i].sArray[j].sName = \"Student_\";\n",
    "            tArray[i].sArray[j].sName += nameSeed[j];\n",
    "\n",
    "            int random = rand() % 61 + 40;  // 40 ~ 100\n",
    "            tArray[i].sArray[j].score = random; //\n",
    "\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "//打印所有信息\n",
    "void printInfo(struct Teacher tArray[], int len)\n",
    "{\n",
    "    for (int i = 0;i<len;i++)\n",
    "    {\n",
    "        cout << \"老师姓名：\" << tArray[i].tName << endl;\n",
    "        \n",
    "        for (int j = 0; j < 5; j++)\n",
    "        {\n",
    "            cout << \"\\t学生姓名：\" << tArray[i].sArray[j].sName \n",
    "                << \" 考试分数：\" << tArray[i].sArray[j].score << endl;\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "    //随机数种子\n",
    "    srand((unsigned int)time(NULL));\n",
    "\n",
    "    //1、创建3名老师的数组\n",
    "    struct Teacher tArray[3];\n",
    "\n",
    "    //2、通过函数给3名老师的信息赋值，并给老师带的学生信息赋值\n",
    "    int len = sizeof(tArray) / sizeof(tArray[0]);\n",
    "    allocateSpace(tArray, len);\n",
    "\n",
    "    //3、打印所有老师及所带的学生信息\n",
    "    printInfo(tArray, len);\n",
    "\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "运行结果：  \n",
    "老师姓名：Teacher_A\n",
    "        学生姓名：Student_A 考试分数：81\n",
    "        学生姓名：Student_B 考试分数：85\n",
    "        学生姓名：Student_C 考试分数：91\n",
    "        学生姓名：Student_D 考试分数：66\n",
    "        学生姓名：Student_E 考试分数：55\n",
    "老师姓名：Teacher_B\n",
    "        学生姓名：Student_A 考试分数：87\n",
    "        学生姓名：Student_B 考试分数：50\n",
    "        学生姓名：Student_C 考试分数：57\n",
    "        学生姓名：Student_D 考试分数：40\n",
    "        学生姓名：Student_E 考试分数：43\n",
    "老师姓名：Teacher_C\n",
    "        学生姓名：Student_A 考试分数：72\n",
    "        学生姓名：Student_B 考试分数：64\n",
    "        学生姓名：Student_C 考试分数：80\n",
    "        学生姓名：Student_D 考试分数：92\n",
    "        学生姓名：Student_E 考试分数：58\n",
    "请按任意键继续. . ."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1.8 结构体案例(二)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "案例描述：设计一个英雄的结构体，包括成员姓名，年龄，性别；创建结构体数组，数组中存放5名英雄。通过冒泡排序算法，将数组中英雄按照年龄进行升序排序，最终打印排序后的结果。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "五名英雄信息如下：  \n",
    " - {\"刘备\",23,\"男\"}  \n",
    " - {\"关羽\",22,\"男\"}  \n",
    " - {\"张飞\",20,\"男\"}  \n",
    " - {\"赵云\",21,\"女\"}  \n",
    " - {\"貂蝉\",19,\"女\"}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#include <iostream>\n",
    "using namespace std;\n",
    "\n",
    "//1、设计英雄结构体\n",
    "struct Hero\n",
    "{\n",
    "    //姓名\n",
    "    string name;\n",
    "    //年龄\n",
    "    int age;\n",
    "    //性别\n",
    "    string sex;\n",
    "};\n",
    "\n",
    "//冒泡排序，实现年龄升序排列\n",
    "void bubbleSort(struct Hero heroArray[], int len)\n",
    "{\n",
    "    for (int i = 0; i < len - 1; i++)\n",
    "    {\n",
    "        for (int j = 0; j < len - i - 1; j++)\n",
    "        {\n",
    "            //如果j下标的年龄大于j+1下标的元素的年龄，交换两个元素\n",
    "            if (heroArray[j].age > heroArray[j + 1].age)\n",
    "            {\n",
    "                struct Hero temp = heroArray[j];  //创建一个临时结构体变量\n",
    "                heroArray[j] = heroArray[j + 1];\n",
    "                heroArray[j + 1] = temp;\n",
    "            }\n",
    "        }\n",
    "    }\n",
    "}\n",
    "\n",
    "void printHero(struct Hero heroArray[], int len)\n",
    "{\n",
    "    for (int i = 0; i < len; i++)\n",
    "    {\n",
    "        cout << \"姓名：\" << heroArray[i].name << \" 年龄：\" << heroArray[i].age\n",
    "            << \" 性别：\" << heroArray[i].sex << endl;\n",
    "    }\n",
    "}\n",
    "\n",
    "int main()\n",
    "{\n",
    "\n",
    "    //2、创建数组存放5名英雄\n",
    "    struct Hero heroArray[5] =\n",
    "    {\n",
    "        {\"刘备\",23,\"男\"},\n",
    "        {\"关羽\",22,\"男\"},\n",
    "        {\"张飞\",20,\"男\"},\n",
    "        {\"赵云\",21,\"女\"},\n",
    "        {\"貂蝉\",19,\"女\"},\n",
    "    };\n",
    "\n",
    "    int len = sizeof(heroArray) / sizeof(heroArray[0]);\n",
    "    cout << \"排序前的打印结果\" << endl;\n",
    "    for (int i = 0; i < len; i++)\n",
    "    {\n",
    "        cout << \"姓名：\" << heroArray[i].name << \" 年龄：\" << heroArray[i].age\n",
    "            << \" 性别：\" << heroArray[i].sex << endl;\n",
    "    }\n",
    "\n",
    "    //3、对数组进行排序，按照年龄进行升序排序\n",
    "    bubbleSort(heroArray, len);\n",
    "\n",
    "    //4、将排序后结果打印输出\n",
    "    cout << \"排序后的打印结果\" << endl;\n",
    "    printHero(heroArray,len);\n",
    "\n",
    "\n",
    "    system(\"pause\");\n",
    "\n",
    "    return 0;\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "运行结果：  \n",
    " - 排序前的打印结果  \n",
    " - 姓名：刘备 年龄：23 性别：男  \n",
    " - 姓名：关羽 年龄：22 性别：男  \n",
    " - 姓名：张飞 年龄：20 性别：男  \n",
    " - 姓名：赵云 年龄：21 性别：女  \n",
    " - 姓名：貂蝉 年龄：19 性别：女  \n",
    " - 排序后的打印结果  \n",
    " - 姓名：貂蝉 年龄：19 性别：女  \n",
    " - 姓名：张飞 年龄：20 性别：男  \n",
    " - 姓名：赵云 年龄：21 性别：女  \n",
    " - 姓名：关羽 年龄：22 性别：男  \n",
    " - 姓名：刘备 年龄：23 性别：男  \n",
    " - 请按任意键继续. . ."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.3",
   "language": "python",
   "name": "python3.6.3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
